﻿using MyUi_WPF_Controls.DataGridCtr.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;

namespace MyUi_WPF_Controls.DataGridCtr
{
    public static class DataGridHelper
    {
        public static readonly DependencyProperty HeadBackgroundProperty = DependencyProperty.RegisterAttached("HeadBackground", typeof(Brush), typeof(DataGridHelper));

        public static readonly DependencyProperty HeadForegroundProperty = DependencyProperty.RegisterAttached("HeadForeground", typeof(Brush), typeof(DataGridHelper));

        public static readonly DependencyProperty HeadFontSizeProperty = DependencyProperty.RegisterAttached("HeadFontSize", typeof(double), typeof(DataGridHelper));

        public static readonly DependencyProperty FrozenColumnBackgroundProperty = DependencyProperty.RegisterAttached("FrozenColumnBackground", typeof(Brush), typeof(DataGridHelper));

        public static readonly DependencyProperty RightFrozenColumnCountProperty = DependencyProperty.RegisterAttached("RightFrozenColumnCount", typeof(int), typeof(DataGridHelper), new PropertyMetadata(0, RightFrozenColumnCountChangedCallback));

        private static readonly DependencyProperty RightFrozenColumnsViewportLengthProperty = DependencyProperty.RegisterAttached("RightFrozenColumnsViewportLength", typeof(double), typeof(DataGridHelper), new PropertyMetadata(0.0));

        public static readonly DependencyProperty DataGridRowHeaderWidthProperty = DependencyProperty.RegisterAttached("DataGridRowHeaderWidth", typeof(double), typeof(DataGridHelper), new PropertyMetadata(30.0));

        public static readonly DependencyProperty FrozenColumnCellsStyleProperty = DependencyProperty.RegisterAttached("FrozenColumnCellsStyle", typeof(Style), typeof(DataGridHelper), new PropertyMetadata(null));

        public static readonly DependencyProperty IsColumnFrozenProperty = DependencyProperty.RegisterAttached("IsColumnFrozen", typeof(bool), typeof(DataGridHelper), new PropertyMetadata(false));

        public static readonly DependencyProperty IsFrozenColumnsEdgeProperty = DependencyProperty.RegisterAttached("IsFrozenColumnsEdge", typeof(bool), typeof(DataGridHelper), new PropertyMetadata(false));

        public static void SetHeadBackground(DependencyObject element, Brush value)
        {
            element.SetValue(HeadBackgroundProperty, value);
        }

        public static Brush GetHeadBackground(DependencyObject element)
        {
            return (Brush)element.GetValue(HeadBackgroundProperty);
        }

        public static void SetHeadForeground(DependencyObject element, Brush value)
        {
            element.SetValue(HeadForegroundProperty, value);
        }

        public static Brush GetHeadForeground(DependencyObject element)
        {
            return (Brush)element.GetValue(HeadForegroundProperty);
        }

        public static void SetHeadFontSize(DependencyObject element, double value)
        {
            element.SetValue(HeadFontSizeProperty, value);
        }

        public static double GetHeadFontSize(DependencyObject element)
        {
            return (double)element.GetValue(HeadFontSizeProperty);
        }

        public static void SetFrozenColumnBackground(DependencyObject element, Brush value)
        {
            element.SetValue(FrozenColumnBackgroundProperty, value);
        }

        public static Brush GetFrozenColumnBackground(DependencyObject element)
        {
            return (Brush)element.GetValue(FrozenColumnBackgroundProperty);
        }

        private static void RightFrozenColumnCountChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            DataGrid obj = (DataGrid)d;
            List<RightFrozenSupportedDataGridCellsPanel> list = obj.FindVisualChildren<RightFrozenSupportedDataGridCellsPanel>();
            foreach (RightFrozenSupportedDataGridCellsPanel item in list)
            {
                item.InvalidateMeasure();
            }
        }

        public static int GetRightFrozenColumnCount(DependencyObject obj)
        {
            return (int)obj.GetValue(RightFrozenColumnCountProperty);
        }

        public static void SetRightFrozenColumnCount(DependencyObject obj, int value)
        {
            obj.SetValue(RightFrozenColumnCountProperty, value);
        }

        public static double GetRightFrozenColumnsViewportLength(DependencyObject obj)
        {
            return (double)obj.GetValue(RightFrozenColumnsViewportLengthProperty);
        }

        public static void SetRightFrozenColumnsViewportLength(DependencyObject obj, double value)
        {
            obj.SetValue(RightFrozenColumnsViewportLengthProperty, value);
        }

        public static double GetDataGridRowHeaderWidth(DependencyObject obj)
        {
            return (double)obj.GetValue(DataGridRowHeaderWidthProperty);
        }

        public static void SetDataGridRowHeaderWidth(DependencyObject obj, double value)
        {
            obj.SetValue(DataGridRowHeaderWidthProperty, value);
        }

        public static Style GetFrozenColumnCellsStyle(DependencyObject obj)
        {
            return (Style)obj.GetValue(FrozenColumnCellsStyleProperty);
        }

        public static void SetFrozenColumnCellsStyle(DependencyObject obj, Style value)
        {
            obj.SetValue(FrozenColumnCellsStyleProperty, value);
        }

        public static bool GetIsColumnFrozen(DependencyObject obj)
        {
            return (bool)obj.GetValue(IsColumnFrozenProperty);
        }

        public static void SetIsColumnFrozen(DependencyObject obj, bool value)
        {
            obj.SetValue(IsColumnFrozenProperty, value);
        }

        public static bool GetIsFrozenColumnsEdge(DependencyObject obj)
        {
            return (bool)obj.GetValue(IsFrozenColumnsEdgeProperty);
        }

        public static void SetIsFrozenColumnsEdge(DependencyObject obj, bool value)
        {
            obj.SetValue(IsFrozenColumnsEdgeProperty, value);
        }

        public static System.Windows.Size SubtractFromSize(System.Windows.Size size, double thickness, bool height)
        {
            return height ? new System.Windows.Size(size.Width, Math.Max(0.0, size.Height - thickness)) : new System.Windows.Size(Math.Max(0.0, size.Width - thickness), size.Height);
        }

        public static bool IsGridLineVisible(DataGrid dataGrid, bool isHorizontal)
        {
            if (dataGrid != null)
            {
                switch (dataGrid.GridLinesVisibility)
                {
                    case DataGridGridLinesVisibility.All:
                        return true;
                    case DataGridGridLinesVisibility.Horizontal:
                        return isHorizontal;
                    case DataGridGridLinesVisibility.None:
                        return false;
                    case DataGridGridLinesVisibility.Vertical:
                        return !isHorizontal;
                }
            }

            return false;
        }

        internal static List<DataGridColumn> GetDisplayColumns(DataGrid dataGrid)
        {
            List<DataGridColumn> list = dataGrid.Columns.ToList();
            list.Sort((DataGridColumn c1, DataGridColumn c2) => c1.DisplayIndex - c2.DisplayIndex);
            return list;
        }
    }
}
